<?php
    include_once('../../src/classes/class.mssql.php');
    
    session_start();

    $comtec_id_mainteiner = $_GET["id"];
    
    $db = new MSSQL();
    
    $tsql = "SELECT comtec_id_user,(name+' '+lastname1+' '+lastname2) as nombre,phone,email,position,access_level 
              FROM [dbo].[users] WHERE comtec_id_mainteiner=$comtec_id_mainteiner and status_reg=1 ORDER BY lastname1 ASC, lastname2 ASC, name ASC";
    $stmt = $db->query($tsql);
           
    $texto  = "<table class='table'>";
    if($_SESSION["users"]=="managers"){ 
     $texto .= "<tr><th colspan=2></th>";
    }elseif ($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians") {
     $texto .= "<tr><th></th>";  
    }
    $texto .= "<th>C&oacute;digo</th>
              <th>Nombre</th>
              <th>Cargo</th>
              <th>Tipo de usuario</th>
              <th>Empresa</th>
              <th>Tel&eacute;fono</th>
              <th>Correo-e</th>
              </tr>";
    $cont = 0;
    while ($row = sqlsrv_fetch_array($stmt,SQLSRV_FETCH_ASSOC)){
     $cont += 1;
     $comtec_id_user=$row['comtec_id_user'];   
     $name=$row['nombre'];
     $phone=$row['phone'];
     $email=$row['email'];
     $position=$row['position'];
     $access_level=$row['access_level'];
     if ($access_level == 5)
     {
      $tsql = "SELECT company_name FROM [dbo].[owner] WHERE comtec_id_user=".$comtec_id_user;   
      $stmt2 = $db->query($tsql);
      $row = sqlsrv_fetch_array($stmt2,SQLSRV_FETCH_ASSOC);
      $empresa = $row['company_name'];    
     }else
         {
          $empresa = "-";
         }
     $texto .= "<tr>";
     if($_SESSION["users"]!="clients" && $_SESSION["users"]!="technicians"){
      $url_edit='http://'.$_SERVER['HTTP_HOST'].'/iconos/edit.png';
      $texto .= "<td><a href='javascript:loadUser(".$comtec_id_user.");'><img src='$url_edit' alt='editar' style='border: 0px;' /></a></td>";
     }
     if($_SESSION["users"]=="managers"){
      $url_delete='http://'.$_SERVER['HTTP_HOST'].'/iconos/delete.png';
      $texto .= "<td><a href='javascript:deleteUser(".$comtec_id_user.");'><img src='$url_delete' alt='eliminar' style='border: 0px;' /></a></td>";
     }
     $texto .= "<td>$comtec_id_user</td>";
     $texto .= "<td>$name</td>";
     $texto .= "<td>$position</td>";
     switch ($access_level) {
         case 1:
                $access_level="Gerente";
         break;
         case 2:
                $access_level="Administrativo-1";
         break;
         case 3:
                $access_level="Administrativo-2";
         break;
         case 4:
                $access_level="T&eacute;cnico";
         break;
         case 5:
                $access_level="Cliente";
         break;    
     }
     $texto .= "<td>$access_level</td>";
     $texto .= "<td>$empresa</td>";
     $texto .= "<td>$phone</td>";
     $texto .= "<td>$email</td>";
     $texto .= "</tr>";
    }
    $texto .= "</table>";
    $texto .= '<div id="result"></div>';
    echo "<p class='total'>Total: $cont</p>".$texto;
?>
